home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / FISCYR15.ARJ / YEAR.C next >
C/C++ Source or Header  |  1992-01-04  |  19KB  |  493 lines

  1.        /*============================================================*/
  2.        /* AUTHOR:   Jim Rickman                                      */
  3.        /* DATE:     January 1992                                     */
  4.        /* PROGRAM:  year.c                                           */
  5.        /*                                                            */
  6.        /* PURPOSE:  Output a Fiscal Calendar for the specified year. */
  7.        /*============================================================*/
  8.  
  9. /*---------------*/
  10. /* Include files */
  11. /*---------------*/
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <stdlib.h>
  15.  
  16. /*-------------*/
  17. /* Definitions */
  18. /*-------------*/
  19. #define TITLE     "──  F I S C A L   C A L E N D A R  ──"
  20. #define TOP_BORDER    "┌─────┬────────────────────────────┬──┐ ┌─────┬────────────────────────────┬──┐"
  21. #define DAY_HDR       "│     │  M   T   W   T   F   S   S │  │ │     │  M   T   W   T   F   S   S │  │"
  22. #define MNTH_HDR      "│Month├────────────────────────────┤F │ │Month├────────────────────────────┤F │"
  23. #define FRST_QTR      "│     │       FIRST   QUARTER      │ W│ │     │       THIRD   QUARTER      │ W│"
  24. #define SCND_QTR      "│     │       SECOND  QUARTER      │ W│ │     │       FOURTH  QUARTER      │ W│"
  25. #define SEP_MONTH     "├─────┼────────────────────────────┼──┤ ├─────┼────────────────────────────┼──┤"
  26. #define BOTTOM_BORDER "└─────┴────────────────────────────┴──┘ └─────┴────────────────────────────┴──┘"
  27.  
  28.  
  29. /*------------------------------------------------------------------*/
  30. /* Number of days for the months of year -- non-leap and leap years */
  31. /*------------------------------------------------------------------*/
  32. static int day_tab[2][13] = { { 0,31,28,31,30,31,30,31,31,30,31,30,31 },
  33.                               { 0,31,29,31,30,31,30,31,31,30,31,30,31 } };
  34. /*------------------------*/
  35. /* Other Global Variables */
  36. /*-------------------------------------------------------------------------*/
  37. struct tm T;    /* If User does NOT specify a Year, use current year       */
  38. time_t Timer;   /* If User does NOT specify a Year, use current year       */
  39. int year;       /* The current or User-specified year                      */
  40. int num_days;   /* Determined by the what_day() function                   */
  41. int day_a;      /* Day value for January 1st                               */
  42. int day_b;      /* Day value for July 1st                                  */
  43. int num_a;      /* Current day value for January through June              */
  44. int num_b;      /* Current day value for July through December             */
  45. int tot_a;      /* Total number of days in a month (January through June)  */
  46. int tot_b;      /* Total number of days in a month (July through December) */
  47. int leap;       /* 0 = Non-Leap Year;  1 = Leap Year                       */
  48. int fw_a;       /* Current Fiscal Week value (January through June)        */
  49. int fw_b;       /* Current Fiscal Week value (July through December)       */
  50. int mnth_a;     /* Current Month value (January through June)              */
  51. int mnth_b;     /* Current Month value (July through December)             */
  52. int lines;      /* Current line number                                     */
  53. int dec_wks;    /* Number of weeks (5 or 6) in December of specified year  */
  54.                 /*---------------------------------------------------------*/
  55.  
  56.  
  57. /*------------------*/
  58. /* The MAIN Routine */
  59. /*------------------*/
  60. main (int argc, char *argv[])
  61. {
  62.     int i;      /* Loop counter                                            */
  63.  
  64.     /*------------------------------------------*/
  65.     /* No year parameter given; do current year */
  66.     /*------------------------------------------*/
  67.     if (argc == 1)
  68.     {
  69.         time (&Timer);
  70.         T = *(localtime (&Timer));
  71.         year = T.tm_year + 1900;                   /* For the 20th Century */
  72.     }
  73.  
  74.     /*---------------------------*/
  75.     /* The user specified a year */
  76.     /*---------------------------*/
  77.     if (argc > 1)
  78.     {
  79.         year = atoi (argv[1]);
  80.         if (year < 1753  ||  year > 3999)
  81.         {
  82.             syntax ();  exit (1);
  83.         }
  84.     }
  85.  
  86.     /*----------------------------------------------------------------*/
  87.     /* Determine what day January 1st of the following year falls on. */
  88.     /* If Jan 1st of the following year falls on a Friday, there are  */
  89.     /* 53 Fiscal Weeks in the year and the month of December has six  */
  90.     /* Fiscal Weeks.                                                  */
  91.     /*----------------------------------------------------------------*/
  92.     year++;  what_day (1);
  93.     if (num_days == 5)
  94.         dec_wks = 6;
  95.     else
  96.         dec_wks = 5;
  97.     year--;
  98.  
  99.     /*---------------------*/
  100.     /* Is it a leap year ? */
  101.     /*---------------------*/
  102.     leap = year % 4 == 0  &&  year % 100 != 0  ||  year % 400 == 0;
  103.  
  104.     /*-------------------------*/
  105.     /* Output the header lines */
  106.     /*-------------------------*/
  107.     printf ("\n                %d  %s  %d\n\n", year, TITLE, year);
  108.     printf ("%s\n%s\n%s\n%s\n%s\n", TOP_BORDER,
  109.                                     DAY_HDR,
  110.                                     MNTH_HDR,
  111.                                     FRST_QTR,
  112.                                     SEP_MONTH);
  113.  
  114.     /*-----------------------------------------------------------------*/
  115.     /* Output the first weeks of January and July.  Once this is done, */
  116.     /* we can output the rest of the year using a loop.                */
  117.     /*-----------------------------------------------------------------*/
  118.     lines = 8;
  119.     /*----------------------------------------------------------*/
  120.     /* Determine the day for January 1st of the specified year: */
  121.     /*----------------------------------------------------------*/
  122.     mnth_a = 1;  what_day (mnth_a);
  123.     /*-----------------------------------------------------------*/
  124.     /* January 1st is on a Monday -- we need to output the first */
  125.     /* week of July locally and NOT via the out_30() function.   */
  126.     /*-----------------------------------------------------------*/
  127.     if (num_days == 1)
  128.     {
  129.         /*----------------------------------*/
  130.         /* Output the first week of January */
  131.         /*----------------------------------*/
  132.         printf ("│     │  1   2   3   4   5   6   7 ");
  133.         day_a = 8;   fw_a = 1;  printf ("│%2d│ ", fw_a);
  134.         /*-------------------------------*/
  135.         /* Output the first week of July */
  136.         /*-------------------------------*/
  137.         if (leap == 0)
  138.         {
  139.             printf ("│     │  2   3   4   5   6   7   8 ");
  140.             day_b = 9;  fw_b = 27;  printf ("│%2d│", fw_b);
  141.         }
  142.         if (leap == 1)
  143.         {
  144.             printf ("│     │  1   2   3   4   5   6   7 ");
  145.             day_b = 8;  fw_b = 27;  printf ("│%2d│", fw_b);
  146.         }
  147.     }
  148.     /*-----------------------------*/
  149.     /* January 1st is on a Tuesday */
  150.     /*-----------------------------*/
  151.     if (num_days == 2  &&  leap == 0)
  152.     {
  153.         /*----------------------------------*/
  154.         /* Output the first week of January */
  155.         /*----------------------------------*/
  156.         printf ("│     │ 31   1   2   3   4   5   6 ");
  157.         day_a = 7;   fw_a = 1;  printf ("│%2d│ ", fw_a);
  158.         /*-------------------------------*/
  159.         /* Output the first week of July */
  160.         /*-------------------------------*/
  161.         if (leap == 0)
  162.         {
  163.             printf ("│     │  1   2   3   4   5   6   7 ");
  164.             day_b = 8;   fw_b = 27;  printf ("│%2d│", fw_b);
  165.         }
  166.         if (leap == 1)
  167.         {
  168.             printf ("│     │ 30   1   2   3   4   5   6 ");
  169.             day_b = 7;   fw_b = 27;  printf ("│%2d│", fw_b);
  170.         }
  171.     }
  172.     /*-------------------------------*/
  173.     /* January 1st is on a Wednesday */
  174.     /*-------------------------------*/
  175.     if (num_days == 3)
  176.     {
  177.         /*----------------------------------*/
  178.         /* Output the first week of January */
  179.         /*----------------------------------*/
  180.         printf ("│     │ 29  30   1   2   3   4   5 ");
  181.         day_a = 6;   fw_a = 1;  printf ("│%2d│ ", fw_a);
  182.         /*-------------------------------*/
  183.         /* Output the first week of July */
  184.         /*-------------------------------*/
  185.         if (leap == 0)
  186.         {
  187.             printf ("│     │ 30   1   2   3   4   5   6 ");
  188.             day_b = 7;   fw_b = 27;  printf ("│%2d│", fw_b);
  189.         }
  190.         if (leap == 1)
  191.         {
  192.             printf ("│     │ 29  30   1   2   3   4   5 ");
  193.             day_b = 6;   fw_b = 27;  printf ("│%2d│", fw_b);
  194.         }
  195.     }
  196.     /*------------------------------*/
  197.     /* January 1st is on a Thursday */
  198.     /*------------------------------*/
  199.     if (num_days == 4)
  200.     {
  201.         /*----------------------------------*/
  202.         /* Output the first week of January */
  203.         /*----------------------------------*/
  204.         printf ("│     │ 29  30  31   1   2   3   4 ");
  205.         day_a = 5;   fw_a = 1;  printf ("│%2d│ ", fw_a);
  206.         /*-------------------------------*/
  207.         /* Output the first week of July */
  208.         /*-------------------------------*/
  209.         if (leap == 0)
  210.         {
  211.             printf ("│     │ 29  30   1   2   3   4   5 ");
  212.             day_b = 6;   fw_b = 27;  printf ("│%2d│", fw_b);
  213.         }
  214.         if (leap == 1)
  215.         {
  216.             printf ("│     │ 28  29  30   1   2   3   4 ");
  217.             day_b = 5;   fw_b = 27;  printf ("│%2d│", fw_b);
  218.         }
  219.     }
  220.     /*----------------------------*/
  221.     /* January 1st is on a Friday */
  222.     /*----------------------------*/
  223.     if (num_days == 5)
  224.     {
  225.         /*----------------------------------*/
  226.         /* Output the first week of January */
  227.         /*----------------------------------*/
  228.         printf ("│     │  4   5   6   7   8   9  10 ");
  229.         day_a = 11;  fw_a = 1;  printf ("│%2d│ ", fw_a);
  230.         /*-------------------------------*/
  231.         /* Output the first week of July */
  232.         /*-------------------------------*/
  233.         if (leap == 0)
  234.         {
  235.             printf ("│     │  5   6   7   8   9  10  11 ");
  236.             day_b = 12;   fw_b = 27;  printf ("│%2d│", fw_b);
  237.         }
  238.         if (leap == 1)
  239.         {
  240.             printf ("│     │  4   5   6   7   8   9  10 ");
  241.             day_b = 11;   fw_b = 27;  printf ("│%2d│", fw_b);
  242.         }
  243.     }
  244.     /*------------------------------*/
  245.     /* January 1st is on a Saturday */
  246.     /*------------------------------*/
  247.     if (num_days == 6)
  248.     {
  249.         /*----------------------------------*/
  250.         /* Output the first week of January */
  251.         /*----------------------------------*/
  252.         printf ("│     │  3   4   5   6   7   8   9 ");
  253.         day_a = 10;  fw_a = 1;  printf ("│%2d│ ", fw_a);
  254.         /*-------------------------------*/
  255.         /* Output the first week of July */
  256.         /*-------------------------------*/
  257.         if (leap == 0)
  258.         {
  259.             printf ("│     │  4   5   6   7   8   9  10 ");
  260.             day_b = 11;  fw_b = 27;  printf ("│%2d│", fw_b);
  261.         }
  262.         if (leap == 1)
  263.         {
  264.             printf ("│     │  3   4   5   6   7   8   9 ");
  265.             day_b = 10;   fw_b = 27;  printf ("│%2d│", fw_b);
  266.         }
  267.     }
  268.     /*----------------------------*/
  269.     /* January 1st is on a Sunday */
  270.     /*----------------------------*/
  271.     if (num_days == 7)
  272.     {
  273.         /*----------------------------------*/
  274.         /* Output the first week of January */
  275.         /*----------------------------------*/
  276.         printf ("│     │  2   3   4   5   6   7   8 ");
  277.         day_a = 9;   fw_a = 1;  printf ("│%2d│ ", fw_a);
  278.         /*-------------------------------*/
  279.         /* Output the first week of July */
  280.         /*-------------------------------*/
  281.         if (leap == 0)
  282.         {
  283.             printf ("│     │  3   4   5   6   7   8   9 ");
  284.             day_b = 10;   fw_b = 27;  printf ("│%2d│", fw_b);
  285.         }
  286.         if (leap ==  1)
  287.         {
  288.             printf ("│     │  2   3   4   5   6   7   8 ");
  289.             day_b = 9;  fw_b = 27;  printf ("│%2d│", fw_b);
  290.         }
  291.     }
  292.     mnth_b = 7;  next_line ();
  293.  
  294.     /*-------------------------------------------*/
  295.     /* Set the variables to their correct values */
  296.     /* before proceeding to the loop.            */
  297.     /*-------------------------------------------*/
  298.     num_a = day_a;  num_b = day_b;  tot_a = 31;  tot_b = 31;
  299.  
  300.     /*-------------------------------------------------------*/
  301.     /* The loop that outputs the rest of the Fiscal Calendar */
  302.     /*-------------------------------------------------------*/
  303.     do
  304.     {   /*-----------------------------------*/
  305.         /* Increment the Fiscal Week numbers */
  306.         /*-----------------------------------*/
  307.         fw_a++;  fw_b++;
  308.         if (fw_b == 53  &&  dec_wks == 5)
  309.             fw_b = 1;
  310.         if (fw_a > 52)
  311.             fw_a = 1;
  312.         for (i=0; i<7; i++, num_a++)
  313.         {
  314.             if (fw_a == 27)
  315.                 printf ("    ");
  316.             if (num_a <= tot_a  &&  fw_a < 27)
  317.                 printf (" %2d ", num_a);
  318.             if (num_a > tot_a  &&  fw_a < 27)
  319.             {
  320.                 num_a = 1;  printf (" %2d ", num_a);
  321.                 mnth_a++;
  322.                 if (mnth_a == 2 && leap == 0)
  323.                     tot_a = 28;
  324.                 if (mnth_a == 2 && leap == 1)
  325.                     tot_a = 29;
  326.                 if (mnth_a == 3 || mnth_a  == 5)
  327.                     tot_a = 31;
  328.                 if (mnth_a == 4 || mnth_a  == 6)
  329.                     tot_a = 30;
  330.             }
  331.         }
  332.         if (fw_a == 27)
  333.             printf ("│  │ ");
  334.         if (fw_a != 27)
  335.             printf ("│%2d│ ", fw_a);
  336.  
  337.         next_half ();
  338.         for (i=0; i<7; i++, num_b++)
  339.         {
  340.             if (num_b <= tot_b)
  341.                 printf (" %2d ", num_b);
  342.             if (num_b > tot_b)
  343.             {
  344.                 num_b = 1;  printf (" %2d ", num_b);
  345.                 mnth_b++;
  346.                 if (mnth_b == 7 || mnth_b  == 8 || mnth_b == 10 || mnth_b == 12)
  347.                     tot_b = 31;
  348.                 if (mnth_b == 9 || mnth_b  == 11)
  349.                     tot_b = 30;
  350.             }
  351.         }
  352.         printf ("│%2d│", fw_b);
  353.         next_line ();
  354.     }
  355.     while (lines < 42);
  356.  
  357.     /*-----------------------*/
  358.     /* Terminate the Program */
  359.     /*-----------------------*/
  360.     exit (0);
  361. }
  362.  
  363.  
  364. /*-------------------------------------------*/
  365. /* Routine to Output the YEAR command Syntax */
  366. /*-------------------------------------------*/
  367. syntax ()
  368. {
  369.     fprintf (stderr, "\nCommand Syntax:  YEAR [year]");
  370.     fprintf (stderr, "\n                 where 'year' is between 1753 and 3999 inclusive.\n");
  371. }
  372.  
  373.  
  374. /*-----------------------------------------------------------------*/
  375. /* Routine to Calculate the Day-Of-Year number  ( 1 -> 365 / 366 ) */
  376. /*-----------------------------------------------------------------*/
  377. day_of_year (int mm, int dd)
  378. {
  379.     int i;      /* Loop counter                                            */
  380.  
  381.     for (i = 1;  i < mm;  i++)
  382.         dd += day_tab[leap][i];
  383.  
  384.     return (dd);
  385. }
  386.  
  387.  
  388. /*----------------------------------------------------------------------*/
  389. /* Routine to Determine the Day Name for the 1st Day of specified Month */
  390. /*----------------------------------------------------------------------*/
  391. what_day (int mnth)
  392. {
  393.     num_days = year + ((year + 3) / 4);
  394.     num_days -= (year - 1701) / 100;
  395.     num_days += (year - 1601) / 400;
  396.     num_days += day_of_year (mnth, 1);
  397.     num_days %= 7;  num_days--;
  398.     if (num_days <= 0)
  399.         num_days = num_days + 7;
  400. }
  401.  
  402.  
  403. /*-----------------------------*/
  404. /* Routine to start a NEW Line */
  405. /*-----------------------------*/
  406. next_line ()
  407. {
  408.     lines++;
  409.     if (lines == 9)
  410.         printf ("\n│ JAN │");
  411.     if (lines == 14)
  412.         printf ("\n│ FEB │");
  413.     if (lines == 19)
  414.         printf ("\n│ MAR │");
  415.     if (lines == 27)
  416.         printf ("\n│ APR │");
  417.     if (lines == 32)
  418.         printf ("\n│ MAY │");
  419.     if (lines == 37)
  420.         printf ("\n│ JUN │");
  421.     if (lines == 11)
  422.         printf ("\n│4 wks│");
  423.     if (lines == 16)
  424.         printf ("\n│4 wks│");
  425.     if (lines == 21)
  426.         printf ("\n│5 wks│");
  427.     if (lines == 29)
  428.         printf ("\n│4 wks│");
  429.     if (lines == 34)
  430.         printf ("\n│4 wks│");
  431.     if (lines == 39)
  432.         printf ("\n│5 wks│");
  433.     if (lines == 12 || lines == 17 || lines == 30 || lines == 35)
  434.     {
  435.         printf ("\n%s", SEP_MONTH);  lines++;
  436.     }
  437.     if (lines == 23)
  438.         lines++;
  439.     if (lines == 24)
  440.     {
  441.         printf ("\n%s\n%s\n%s\n%s\n%s\n%s", BOTTOM_BORDER,
  442.                                             TOP_BORDER,
  443.                                             DAY_HDR,
  444.                                             MNTH_HDR,
  445.                                             SCND_QTR,
  446.                                             SEP_MONTH);
  447.         lines++;  lines++;
  448.     }
  449.     if (lines == 10 || lines == 13 || lines == 15 || lines == 18 ||
  450.         lines == 20 || lines == 22 || lines == 26 || lines == 28 ||
  451.         lines == 31 || lines == 33 || lines == 36 || lines == 38 ||
  452.         lines == 40 || lines == 41)
  453.         printf ("\n│     │");
  454.     if (lines == 42)
  455.         printf ("\n%s\n", BOTTOM_BORDER);
  456. }
  457.  
  458.  
  459. /*------------------------------------------------------*/
  460. /* Routine to start the second half of the CURRENT line */
  461. /*------------------------------------------------------*/
  462. next_half ()
  463. {
  464.     if (lines == 9)
  465.         printf ("│ JUL │");
  466.     else if (lines == 14)
  467.         printf ("│ AUG │");
  468.     else if (lines == 19)
  469.         printf ("│ SEP │");
  470.     else if (lines == 27)
  471.         printf ("│ OCT │");
  472.     else if (lines == 32)
  473.         printf ("│ NOV │");
  474.     else if (lines == 37)
  475.         printf ("│ DEC │");
  476.     else if (lines == 11)
  477.         printf ("│4 wks│");
  478.     else if (lines == 16)
  479.         printf ("│4 wks│");
  480.     else if (lines == 21)
  481.         printf ("│5 wks│");
  482.     else if (lines == 29)
  483.         printf ("│4 wks│");
  484.     else if (lines == 34)
  485.         printf ("│4 wks│");
  486.     else if (lines == 39  &&  dec_wks == 5)
  487.         printf ("│5 wks│");
  488.     else if (lines == 39  &&  dec_wks == 6)
  489.         printf ("│6 wks│");
  490.     else
  491.         printf ("│     │");
  492. }
  493.